package com.command;

import com.base.service.ApplicationService;
import com.framework.util.StringUtil;
import com.framework.view.Session;
import com.utils.GsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *  不带/soa的请求的拦截器
 */
public class HandlerFrontInterceptor extends HandlerInterceptorAdapter {
    @Resource(name="applicationServiceImpl")
    ApplicationService applicationService;
    @Override
    public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path=request.getContextPath();
        String url= StringUtils.remove(request.getRequestURI(), request.getContextPath());

        //若果是静态资源
        if(url != null && url.contains("WEB-INF") ){
            response.sendRedirect(request.getContextPath()+"/front/static/index?prefix="+"404");
            return false;
        }
        //排除不需要拦截的
        if(unHandle(url)){
            return true;
        }
        return true;
    }

    @Override
    public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    /**
     *
     * @param url
     * @return 返回true 不拦截  返回false需要拦截
     */
    private boolean unHandle(String url){
        if(url.equals("/")){
            return true;
        }
        //登录注册
        if(url.contains("/front/login")){
            return true;
        }
        if(url.contains("/front/layer")){
            return true;
        }
        //静态资源
        if(url.contains("/front/static")){
            return true;
        }
        if(url.contains("/img")){
            return true;
        }
        //css不拦截
        if(url.contains("/css")){
            return true;
        }
        //js不拦截
        if(url.contains("/js")){
            return true;
        }
        //lib
        if(url.contains("/lib")){
            return true;
        }
        return false;
    }
}
